package de.stefanocke.roo.wicket.base.html

import java.util.List
import org.springframework.roo.support.util.XmlUtils
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node

class HtmlBuilder<M> {

	Document document
	
	M metadata
	
	def getMetadata(){
		metadata
	}
	
	def getDocument(){
		if(document == null){
			val builder = XmlUtils::documentBuilder
			document = builder.newDocument
		}
		document
	}
	
	def element(String name){
		getDocument.createElement(name);
	}
	
	def attr(Element element, String name, String value){
		element.setAttribute(name, value)
		element
	}
	
	def children(Element element, List<? extends Node> children){
		for(node : children){
			element.appendChild(node);
		}
		element
	}
	def children(Element element, Node child){
		element.appendChild(child);
		element
	}
	
	def html(){
		element("html")
			.attr("xmlns","http://www.w3.org/1999/xhtml")
			.attr("xmlns:wicket","http://wicket.sourceforge.net/")
			.attr("xmlns:roo","http://roo-wicket.googlecode.com/");
		
		//wicket:extend xmlns:wicket="http://wicket.sourceforge.net/"
	}
	
	def body(){
		element("body").attr("roo:findByName","true");
	}
	
	def extend(){
		element("wicket:extend").attr("roo:id","extend");
	}
	
	def span(String wicketId){
		element("span").attr("wicket:id",wicketId).attr("roo:id",wicketId)
	}	
	
	def init(M metadata){
		this.metadata = metadata
		this
	}
	
	def Document build(){}	
}